package cn.ickck.law_paper_final.controller;

import cn.ickck.law_paper_final.model.entity.CreditCard;
import cn.ickck.law_paper_final.model.entity.CreditCardBatch;
import cn.ickck.law_paper_final.model.response.CreditCardResponse;
import cn.ickck.law_paper_final.service.CreditCardBatchService;
import cn.ickck.law_paper_final.service.CreditCardService;
import cn.ickck.law_paper_final.utils.ExcelUtils;
import cn.ickck.law_paper_final.utils.JsonData;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author: ck
 * @Description:
 * @Date: Create in 16:26 2020/10/17
 */
@RestController
@RequestMapping("/api/pub/v2/credit_card")
public class CreditCardController {
    @Autowired
    private CreditCardService creditCardService;

    @Autowired
    private CreditCardBatchService creditCardBatchService;

    @RequestMapping("selectByPage")
    public JsonData selectByPage(@Param("page") Integer page) {
        int count = creditCardService.count();
        CreditCardResponse creditCardResponse = new CreditCardResponse();
        creditCardResponse.setCount(count);
        int start = count - page * 20 > 0 ? count - page * 20 : 0;
        int end = count - (page - 1) * 20 > 0 ? count - (page - 1) * 20 : count;
        List<CreditCard> creditCards = creditCardService.selectByPage(start, end);
        creditCardResponse.setCreditCards(creditCards);
        return JsonData.buildSuccess(creditCardResponse);
    }

    @RequestMapping("findAll")
    public JsonData findAll() {
        return JsonData.buildSuccess(creditCardService.findAll());
    }

    @RequestMapping("deleteById")
    public JsonData deleteById(Integer id) {
        creditCardService.deleteById(id);
        return JsonData.buildSuccess("删除成功");
    }


    @Transactional
    @RequestMapping("insert")
    public JsonData insert(@RequestBody CreditCard creditCard) {
        if(creditCard.getOpenDate()==null)
        {
            creditCard.setOpenDate(new Date(200,4,20));
        }
        if(creditCard.getDeadlineDate()==null)
        {
            creditCard.setDeadlineDate(new Date(200,4,20));
        }
        if(creditCard.getBirthDate()==null)
        {
            creditCard.setBirthDate(new Date(200,4,20));
        }
        if(creditCard.getPaperSignDate()==null)
        {
            creditCard.setPaperSignDate(new Date(200,4,20));
        }
        if(creditCard.getStartOverDate()==null)
        {
            creditCard.setStartOverDate(new Date(200,4,20));
        }
        if(creditCard.getLastConsumeDate()==null)
        {
            creditCard.setLastConsumeDate(new Date(200,4,20));
        }
        if(creditCard.getLastPayDate()==null)
        {
            creditCard.setLastPayDate(new Date(200,4,20));
        }
        CreditCardBatch creditCardBatch = new CreditCardBatch();
        creditCardBatch.setNum(String.valueOf(1));
        String key = UUID.randomUUID().toString();
        creditCardBatch.setName(key);
        creditCardBatch.setKey(key);
        creditCardBatch.setDate(new Date());
        creditCardBatchService.insert(creditCardBatch);
        creditCard.setKey(key);
        creditCardService.insert(creditCard);

        return JsonData.buildSuccess("插入成功");
    }

    //考虑是否更新批次日期
    @RequestMapping("update")
    public JsonData update(@RequestBody CreditCard creditCard) {
        creditCardService.update(creditCard);
        return JsonData.buildSuccess();
    }


    //应该加入事务进行控制
    @Transactional
    @RequestMapping("insert_creditCards")
    public JsonData insertCreditCards(@RequestParam("file") MultipartFile file) throws Exception {

        //解析excel


        List<CreditCard> creditCards = ExcelUtils.importExcel(file, 0, 1, CreditCard.class);
        for (CreditCard creditCard:creditCards)
        {
            if(creditCard.getOpenDate()==null)
            {
                creditCard.setOpenDate(new Date(200,4,20));
            }
            if(creditCard.getDeadlineDate()==null)
            {
                creditCard.setDeadlineDate(new Date(200,4,20));
            }
            if(creditCard.getBirthDate()==null)
            {
                creditCard.setBirthDate(new Date(200,4,20));
            }
            if(creditCard.getPaperSignDate()==null)
            {
                creditCard.setPaperSignDate(new Date(200,4,20));
            }
            if(creditCard.getStartOverDate()==null)
            {
                creditCard.setStartOverDate(new Date(200,4,20));
            }
            if(creditCard.getLastConsumeDate()==null)
            {
                creditCard.setLastConsumeDate(new Date(200,4,20));
            }
            if(creditCard.getLastPayDate()==null)
            {
                creditCard.setLastPayDate(new Date(200,4,20));
            }
        }

        CreditCardBatch creditCardBatch = new CreditCardBatch();
        creditCardBatch.setNum(String.valueOf(creditCards.size()));
        String key = UUID.randomUUID().toString();
        creditCardBatch.setName(key);
        creditCardBatch.setKey(key);
        creditCardBatch.setDate(new Date());
        creditCardBatchService.insert(creditCardBatch);
        for (CreditCard creditCard : creditCards) {
            creditCard.setKey(key);
            creditCardService.insert(creditCard);
        }


        return JsonData.buildSuccess("批量录入成功");
    }


    @RequestMapping("findByNameOrKey")
    public JsonData findByNameOrKey(@Param("key") String key, @Param("page") Integer page) {
        CreditCardResponse creditCardResponse = new CreditCardResponse();
        List<CreditCard> creditCards = creditCardService.findByNameOrKey(key);
        creditCardResponse.setCount(creditCards.size());
        List<CreditCard> creditCards1 = new ArrayList<>();

        int start = creditCards.size() - page * 20 > 0 ? creditCards.size() - page * 20 : 0;
        int end = creditCards.size() - (page - 1) * 20 > 0 ? creditCards.size() - (page - 1) * 20 : creditCards.size();

        for (int i = start; i < end; i++) {
            creditCards1.add(creditCards.get(i));
        }
        creditCardResponse.setCreditCards(creditCards1);
        return JsonData.buildSuccess(creditCardResponse);
    }


}
